import { createRouter, createWebHistory } from 'vue-router'

const Home = ()=> import('views/Home.vue');
const About = ()=> import('views/About.vue');
const ShopCar = ()=> import('views/ShopCar.vue');
const Cate = ()=> import('views/Cate.vue');
const Detail = ()=> import('views/Detail.vue');
const Reg = ()=> import('views/user/Register.vue');
const Login = ()=> import('views/user/Login.vue');
const Address = ()=> import('views/user/Address.vue');
const AddressEdit = ()=> import('views/user/AddressEdit.vue');
const Addorder = ()=> import('views/user/Addorder.vue');
const Order = ()=> import('views/order/Order.vue');
const OrderDetail = ()=> import('views/order/OrderDetail.vue');
const Addcp = ()=> import('views/user/Addcp.vue');
const Contact = ()=> import('views/Contact.vue');

import store from '../store';
import { Toast } from 'vant';


const routes = [
  {
    path: '/',
    name: 'DefaultHome',
    // redirect:{name:'home'},
    component: Home,
    meta:{
      title:'我的商城'
    }
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    meta:{
      title:'我的商城-首页'
    }
  },
  {
    path: '/cate',
    name: 'cate',
    component: Cate,
    meta:{
      title:'我的商城-分类'
    }
  },
  {
    path: '/car',
    name: 'car',
    component: ShopCar,
    meta:{
      title:'我的商城-购物车',
      isAuth:true
    }
  },
  {
    path: '/about',
    name: 'about',
    component: About,
    meta:{
      title:'我的商城-个人中心',
      isAuth:true
    }
  },
  {
    path: '/detail',
    name: 'detail',
    component: Detail,
    meta:{
      title:'我的商城-商品详情'
    }
  },
  {
    path: '/reg',
    name: 'reg',
    component: Reg,
    meta:{
      title:'我的商城-注册'
    }
  },
  {
    path: '/login',
    name: 'login',
    component: Login,
    meta:{
      title:'我的商城-登陆'
    }
  },
  {
    path: '/address',
    name: 'address',
    component: Address,
    meta:{
      title:'我的商城-我的地址',
      isAuth:true
    }
  },
  {
    path: '/addressedit',
    name: 'addressedit',
    component: AddressEdit,
    meta:{
      title:'我的商城-添加地址',
      isAuth:true
    }
  },
  {
    path: '/addorder',
    name: 'addorder',
    component: Addorder,
    meta:{
      title:'我的商城-订单',
      isAuth:true
    }
  },
  {
    path: '/order',
    name: 'order',
    component: Order,
    meta:{
      title:'我的商城-订单列表',
      isAuth:true
    }
  },
  {
    path: '/orderdetail',
    name: 'orderdetail',
    component: OrderDetail,
    meta:{
      title:'我的商城-订单详情',
      isAuth:true
    }
  },
  {
    path: '/addcp',
    name: 'addcp',
    component: Addcp,
    meta:{
      title:'我的商城-我的收藏',
      isAuth:true
    }
  },
  {
    path: '/contact',
    name: 'contact',
    component: Contact,
    meta:{
      title:'我的商城-联系方式',
      isAuth:true
    }
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to,from,next)=>{     //通过导航守卫加网站title
  //如果没有登陆，在这里到login
  if(to.meta.isAuth && store.state.user.isLogin === false ){
    Toast.fail('请先登陆');
    return next('/login');
  }
  next();
  document.title = to.meta.title;
})

export default router
